<?php

namespace frontend\controllers;

use Yii;
use frontend\models\Country;
use frontend\models\CountrySearch;
use frontend\models\OrderSearch;
use frontend\models\NameForm;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;


/**
 * CountryController implements the CRUD actions for Country model.
 */
class CountryController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Country models.
     * @return mixed
     */
    public function actionIndex()
    {
        Yii::info("test1");
        $searchModel = new CountrySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        $searchModel2 = new OrderSearch();
        $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams);
        Yii::info("test12222222222");

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,

            'searchModel2' => $searchModel2,
            'dataProvider2' => $dataProvider2,
        ]);
    }


    public function actionOrders()
    {
        $searchModel = new OrderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        if(array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX']){
            $this->renderPartial('orders', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);

        }else{
            return $this->render('orders', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);
        }
    }

    /**
     * Displays a single Country model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Country model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Country();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Country model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Country model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Country model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Country the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Country::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


    public function actionNames()
    {
        $model = new NameForm();
        if ($model->load(Yii::$app->request->post())) {
            if(Yii::$app->request->isAjax){
                Yii::$app->response->format = Response::FORMAT_JSON;
                return \yii\widgets\ActiveForm::validate($model);

            }
            if (true) {
                Yii::$app->session->setFlash('success', 'success 123');
            } else {
                Yii::$app->session->setFlash('error', 'error 456');
            }

            return $this->refresh();
        } else if(Yii::$app->request->isAjax){
            return $this->renderAjax('name', [
                'model' => $model,
            ]);
        }  else {
            return $this->render('name', [
                'model' => $model,
            ]);
        }
    }

    public function actionCode()
    {
        $countries = Yii::$app->request->post('country');
        $index = Yii::$app->request->post('editableIndex');
        $id = Yii::$app->request->post('editableKey');
        $attr = Yii::$app->request->post('editableAttribute');

        $model = $this->findModel($id);

        $out = ['output'=>$countries[$index][$attr],'message'=>''];
        if($model->load($countries[$index])){
            $model->save();
            $out = ['output'=>$model->code,'message'=>''];
        }
        //输出的是output会替换原来的数值显示
        //message如果出错了是错误提示信息，没有出差设置为空!
        

        return json_encode($out);
    }

    /**
     * 
     */
    public function actionModal()
    {
        $m = CountrySearch::find();
        // $m->na
        return $this->render('model');
    }

    public function actionTtest($id=null,$name=null)
    {
        $this->signValidat();
        return "";
    }

    protected function signValidat()
    {
        $params = $this->actionParams;//能不能获取到post的数据???
        var_dump($params);

        Yii::$app->end();
    }
}
